package com.bstek.uflo.console.view.security.filter.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.bstek.dorado.view.ViewElement;
import com.bstek.dorado.view.widget.form.autoform.AutoFormElement;
import com.bstek.uflo.console.view.security.filter.AbstractElementFilter;
import com.bstek.uflo.process.security.Authority;
import com.bstek.uflo.process.security.ComponentAuthority;

/**
 * @author Jacky.gao
 * @since 2013年10月21日
 */
@Component("uflo.console.formElementFilter")
public class FormElementFilter extends AbstractElementFilter {

	public boolean support(ViewElement element) {
		return element instanceof AutoFormElement;
	}

	public void filter(ViewElement viewElement,List<ComponentAuthority> componentAuthorities) {
		AutoFormElement element=(AutoFormElement)viewElement;
		String id=element.getId();
		Authority auth=filterComponent(id,componentAuthorities);
		if(auth!=null){
			if(auth.equals(Authority.Read)){
				element.setReadOnly(true);
			}else if(auth.equals(Authority.Hide)){
				element.setIgnored(true);
			}
			return;
		}
		String name=element.getName();
		auth=filterComponent(name,componentAuthorities);
		if(auth!=null){
			if(auth.equals(Authority.Read)){
				element.setReadOnly(true);
			}else if(auth.equals(Authority.Hide)){
				element.setIgnored(true);
			}
			return;
		}
		String label=element.getLabel();
		auth=filterComponent(label,componentAuthorities);
		if(auth!=null){
			if(auth.equals(Authority.Read)){
				element.setReadOnly(true);
			}else if(auth.equals(Authority.Hide)){
				element.setIgnored(true);
			}
		}
	}
}
